package com.wholesale;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

import com.wholesale.datas.DbHelperAdapter;
import com.wholesale.utils.Constants;
import com.wholesale.utils.UpdateUIService;

import dalvik.system.VMRuntime;

public class WholeSale extends TabActivity implements
		android.widget.CompoundButton.OnCheckedChangeListener {
	private RadioGroup group;
	private TabHost tabHost;
	public static final String TAB_WHOLESALE = "tab_wholeSale";
	public static final String TAB_LIST = "tab_list";
	public static final String TAB_UNDER = "tab_under";
	public static final String TAB_CART = "tab_cart";
	public static final String TAB_MORE = "tab_more";

	private RadioButton wholeSaleBtn;
	private RadioButton listBtn;
	private RadioButton underBtn;
	private RadioButton cartBtn;
	private RadioButton moreBtn;
	DataReceiver dataReceiver;// BroadcastReceiver对象
	private ImageView shopCarView;
	private final static float TARGET_HEAP_UTILIZATION = 0.75f; 
	private Drawable wholeSaleOn ,wholeSaleOff,listOn,listOff,under3On,under3Off,cartOn,cartOff,moreOn,moreOff ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintabs);
		int newSize = 6 * 1024 * 1024 ; //设置最小堆内存大小为6MB
		VMRuntime.getRuntime().setMinimumHeapSize(newSize);
		VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); // 设置堆内存的利用率为75%
		group = (RadioGroup) findViewById(R.id.main_radio);
		shopCarView = (ImageView) findViewById(R.id.shopcar_num);
		initRadios();
		setupIntent();
		setShopCartNumber();
		Intent myIntent = new Intent(this,
				com.wholesale.utils.UpdateUIService.class);
		this.startService(myIntent);
		wholeSaleOn = this.getResources().getDrawable(R.drawable.wholesale_btn_on);
		wholeSaleOff = this.getResources().getDrawable(R.drawable.wholesale_btn_off);
		listOn = this.getResources().getDrawable(R.drawable.list_btn_on);
		listOff = this.getResources().getDrawable(R.drawable.list_btn_off);
		under3On = this.getResources().getDrawable(R.drawable.under_btn_on);
		under3Off = this.getResources().getDrawable(R.drawable.under_btn_off);
		cartOn = this.getResources().getDrawable(R.drawable.cart_btn_on);
		cartOff = this.getResources().getDrawable(R.drawable.cart_btn_off);
		moreOn = this.getResources().getDrawable(R.drawable.more_btn_on);
		moreOff = this.getResources().getDrawable(R.drawable.more_btn_off);
//		wholeSaleBtn.setBackgroundResource(R.drawable.wholesale_btn_on);
		wholeSaleBtn.setBackgroundDrawable(wholeSaleOn);
		this.startService(new Intent(this, UpdateUIService.class));
		
		Constants.appContext = this;
	}

	private void setShopCartNumber() {
		DbHelperAdapter dbHelperAdapter = new DbHelperAdapter(this);
		dbHelperAdapter.open();
		int count = dbHelperAdapter.getGoodsCount();
		dbHelperAdapter.close();
		if (count > 0) {
			shopCarView.setVisibility(View.VISIBLE);
			shopCarView.setImageResource(R.drawable.number_bg);
			Bitmap shopCart = BitmapFactory.decodeResource(this.getResources(),
					R.drawable.number_bg);
			Bitmap newBit = Bitmap.createBitmap(shopCart.getWidth(), shopCart
					.getHeight(), Config.ARGB_8888);
			Canvas canvasTemp = new Canvas(newBit);
			canvasTemp.drawBitmap(shopCart, 0, 0, null);
			// count = 99 ;
			Paint p = new Paint();
			String familyName = "宋体";
			Typeface font = Typeface.create(familyName, Typeface.BOLD);
			p.setColor(Color.WHITE);
			p.setTypeface(font);
			int strwid = 0;
			int textSize = 14;
			do {
				p.setTextSize(textSize);
				strwid = (int) p.measureText("" + count);
				textSize = textSize - 2;
			} while (strwid > 20);
			Rect rect = new Rect();
			// 返回包围整个字符串的最小的一个Rect区域
			p.getTextBounds("" + count, 0, 1, rect);
			canvasTemp.drawText("" + count, (shopCart.getWidth() - strwid) / 2,
					(shopCart.getHeight()) / 2 + 2, p);
			shopCarView.setImageBitmap(newBit);
			shopCarView.postInvalidate();
		} else {
			shopCarView.setVisibility(View.INVISIBLE);
		}
	}

	/**
	 * 初始化底部按钮
	 */
	private void initRadios() {
		wholeSaleBtn = (RadioButton) findViewById(R.id.radio_button0);
		wholeSaleBtn.setOnCheckedChangeListener(this);
		listBtn = (RadioButton) findViewById(R.id.radio_button1);
		listBtn.setOnCheckedChangeListener(this);
		underBtn = (RadioButton) findViewById(R.id.radio_button2);
		underBtn.setOnCheckedChangeListener(this);
		cartBtn = (RadioButton) findViewById(R.id.radio_button3);
		cartBtn.setOnCheckedChangeListener(this);
		moreBtn = (RadioButton) findViewById(R.id.radio_button4);
		moreBtn.setOnCheckedChangeListener(this);
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_button0:
//				wholeSaleBtn.setBackgroundResource(R.drawable.wholesale_btn_on);
//				listBtn.setBackgroundResource(R.drawable.list_btn_off);
//				underBtn.setBackgroundResource(R.drawable.under_btn_off);
//				cartBtn.setBackgroundResource(R.drawable.cart_btn_off);
//				moreBtn.setBackgroundResource(R.drawable.more_btn_off);
				
				wholeSaleBtn.setBackgroundDrawable(wholeSaleOn);
				listBtn.setBackgroundDrawable(listOff);
				underBtn.setBackgroundDrawable(under3Off);
				cartBtn.setBackgroundDrawable(cartOff);
				moreBtn.setBackgroundDrawable(moreOff);
				tabHost.setCurrentTabByTag(TAB_WHOLESALE);
				break;
			case R.id.radio_button1:
//				wholeSaleBtn
//						.setBackgroundResource(R.drawable.wholesale_btn_off);
//				listBtn.setBackgroundResource(R.drawable.list_btn_on);
//				underBtn.setBackgroundResource(R.drawable.under_btn_off);
//				cartBtn.setBackgroundResource(R.drawable.cart_btn_off);
//				moreBtn.setBackgroundResource(R.drawable.more_btn_off);
				wholeSaleBtn.setBackgroundDrawable(wholeSaleOff);
				listBtn.setBackgroundDrawable(listOn);
				underBtn.setBackgroundDrawable(under3Off);
				cartBtn.setBackgroundDrawable(cartOff);
				moreBtn.setBackgroundDrawable(moreOff);
				tabHost.setCurrentTabByTag(TAB_LIST);
				break;
			case R.id.radio_button2:
//				wholeSaleBtn
//						.setBackgroundResource(R.drawable.wholesale_btn_off);
//				listBtn.setBackgroundResource(R.drawable.list_btn_off);
//				underBtn.setBackgroundResource(R.drawable.under_btn_on);
//				cartBtn.setBackgroundResource(R.drawable.cart_btn_off);
//				moreBtn.setBackgroundResource(R.drawable.more_btn_off);
				wholeSaleBtn.setBackgroundDrawable(wholeSaleOff);
				listBtn.setBackgroundDrawable(listOff);
				underBtn.setBackgroundDrawable(under3On);
				cartBtn.setBackgroundDrawable(cartOff);
				moreBtn.setBackgroundDrawable(moreOff);
				tabHost.setCurrentTabByTag(TAB_UNDER);
				break;
			case R.id.radio_button3:
//				wholeSaleBtn
//						.setBackgroundResource(R.drawable.wholesale_btn_off);
//				listBtn.setBackgroundResource(R.drawable.list_btn_off);
//				underBtn.setBackgroundResource(R.drawable.under_btn_off);
//				cartBtn.setBackgroundResource(R.drawable.cart_btn_on);
//				moreBtn.setBackgroundResource(R.drawable.more_btn_off);
				wholeSaleBtn.setBackgroundDrawable(wholeSaleOff);
				listBtn.setBackgroundDrawable(listOff);
				underBtn.setBackgroundDrawable(under3Off);
				cartBtn.setBackgroundDrawable(cartOn);
				moreBtn.setBackgroundDrawable(moreOff);
				tabHost.setCurrentTabByTag(TAB_CART);
				break;
			case R.id.radio_button4:
//				wholeSaleBtn
//						.setBackgroundResource(R.drawable.wholesale_btn_off);
//				listBtn.setBackgroundResource(R.drawable.list_btn_off);
//				underBtn.setBackgroundResource(R.drawable.under_btn_off);
//				cartBtn.setBackgroundResource(R.drawable.cart_btn_off);
//				moreBtn.setBackgroundResource(R.drawable.more_btn_on);
				wholeSaleBtn.setBackgroundDrawable(wholeSaleOff);
				listBtn.setBackgroundDrawable(listOff);
				underBtn.setBackgroundDrawable(under3Off);
				cartBtn.setBackgroundDrawable(cartOff);
				moreBtn.setBackgroundDrawable(moreOn);
				tabHost.setCurrentTabByTag(TAB_MORE);
				break;
			default:
				break;
			}
		}
	}

	private void setupIntent() {
		tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec(TAB_WHOLESALE).setIndicator(
				TAB_WHOLESALE).setContent(
				new Intent(this, WholeSaleActivity.class)));
		tabHost.addTab(tabHost.newTabSpec(TAB_LIST).setIndicator(TAB_LIST)
				.setContent(new Intent(this, ListActivity.class)));
		tabHost.addTab(tabHost.newTabSpec(TAB_UNDER).setIndicator(TAB_UNDER)
				.setContent(new Intent(this, UnderActivity.class)));
		tabHost.addTab(tabHost.newTabSpec(TAB_CART).setIndicator(TAB_CART)
				.setContent(new Intent(this, CartActivity.class)));
		tabHost.addTab(tabHost.newTabSpec(TAB_MORE).setIndicator(TAB_MORE)
				.setContent(new Intent(this, MoreActivity.class)));
		tabHost.setCurrentTab(0);
	}

	private class DataReceiver extends BroadcastReceiver {// 继承自BroadcastReceiver的子类
		@Override
		public void onReceive(Context context, Intent intent) {
			int cmd = intent.getIntExtra("cmd", -1);// 获取Extra信息
			if (cmd == Constants.CMD_MYACCOUNT_SERVICE) {
				wholeSaleBtn
						.setBackgroundResource(R.drawable.wholesale_btn_off);
				listBtn.setBackgroundResource(R.drawable.list_btn_off);
				underBtn.setBackgroundResource(R.drawable.under_btn_off);
				cartBtn.setBackgroundResource(R.drawable.cart_btn_off);
				moreBtn.setBackgroundResource(R.drawable.more_btn_on);
				Constants.toMyAccount = true ;
				tabHost.setCurrentTabByTag(TAB_MORE);
			} else {
				setShopCartNumber();
			}
		}
	}

	@Override
	protected void onStart() {// 重写onStart方法
		dataReceiver = new DataReceiver();
		IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
		filter.addAction("com.wholesale.WholeSale");
		registerReceiver(dataReceiver, filter);// 注册Broadcast Receiver
		super.onStart();
	}

	@Override
	protected void onStop() {// 重写onStop方法
		unregisterReceiver(dataReceiver);// 取消注册Broadcast Receiver
		super.onStop();
	}

	@Override
	public void onDestroy() {// 重写onDestroy方法
		Intent myIntent = new Intent();// 创建Intent对象
		myIntent.setAction("com.wholesale.utils.UpdateUIService");
		myIntent.putExtra("cmd", Constants.CMD_STOP_SERVICE);
		myIntent.putExtra("from", "com.wholesale.wholeSale");
		sendBroadcast(myIntent);
		this.stopService(new Intent(this, UpdateUIService.class));
		super.onDestroy();
	}
}